package ip.ejb.sequence;

import javax.ejb.*;

/**
 * @ejb.bean
 *   cmp-version="2.x"
 *   name="Sequence"
 *   type="CMP"
 *   jndi-name="ejb/util/Sequence"
 *   local-jndi-name="ejb/util/SequenceLocal"
 *   reentrant="false"
 * 
 * @ejb.persistence
 *   table-name="sequence"
 * 
 * @ejb.transaction
 * 	 type="Required"
 * 
 * @jboss.table-name
 *   table-name="sequence"
 * 
 * @jboss.create-table
 *   create="true"
 * 
 * @jboss.remove-table
 *   remove="false"
 *
 */
public abstract class SequenceBean implements EntityBean {

  public SequenceBean(){
  }

  /**
   * @ejb.persistence column-name="name"
   * @ejb.interface-method view-type="local"
   * @ejb.pk-field
   * @jboss.jdbc-type type="VARCHAR"
   * @jboss.sql-type  type="VARCHAR(50)"
   */
  public abstract String getSequenceName();
  /**
   * @ejb.interface-method view-type="local"
   */
  public abstract void setSequenceName(String sequenceName);

  /**
   * @ejb.persistence column-name="current"
   * @ejb.interface-method view-type="local"
   * @jboss.jdbc-type type="INTEGER"
   * @jboss.sql-type  type="INTEGER"
   */
  public abstract int getCurrentValue();
  /**
   * @ejb.interface-method view-type="local"
   */
  public abstract void setCurrentValue(int currentValue);

  public void ejbActivate() {
  }

  public void ejbRemove() throws RemoveException{
  }

  public void ejbPassivate() {
  }

  public void ejbLoad() {
  }

  public void ejbStore() {
  }

  public void setEntityContext(EntityContext ctx) {
    this.ctx = ctx;
  }

  public void unsetEntityContext() {
    this.ctx = null;
  }

  public void ejbPostCreate(String sequenceName, int currentValue) {
  }

  /**
   * @ejb.create-method
   */
  public ip.ejb.sequence.SequencePK ejbCreate(String sequenceName, int currentValue) throws CreateException {
    setSequenceName(sequenceName);
    setCurrentValue(currentValue);
    return new ip.ejb.sequence.SequencePK(sequenceName);
  }
  /**
   * @ejb.interface-method view-type="local"
   */
  public int getBlock(int blockSize) {
    int retVal = getCurrentValue();
    setCurrentValue(retVal + blockSize);
    return retVal;
  }

  protected EntityContext ctx;
}